﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class CurrentAuctions : System.Web.UI.Page
{

    static int MAXSIZE = 50;
    string temp, temp2;
    int Rows, Columns, i;
    int[] AuctionIDNum = new int[MAXSIZE];
    string[] Seller = new string[MAXSIZE];
    string[] AuctionTitle = new string[MAXSIZE];
    int[] ItemIDNum = new int[MAXSIZE];
    DateTime[] Endtime = new DateTime[MAXSIZE];
    string[] CurrentWinner = new string[MAXSIZE];
    string[] Bidprice = new string[MAXSIZE];
    string[] Picture = new string[MAXSIZE];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] != null)
        {
            string ongoingAuctions = null;
            string auctionsAwaitingPayment = null;
            string completedAuctions = null;
            int whatTable = 1;

            ongoingAuctions = "Select * FROM Auctions WHERE currentWinner='" + Session["userID"] + "' and closed='no'";
            auctionsAwaitingPayment = "Select * FROM Auctions WHERE currentWinner='" + Session["userID"] + "' and closed='yes' and paid='no'";
            completedAuctions = "Select * FROM Auctions WHERE currentWinner='" + Session["userID"] + "' and closed='yes' and paid='yes'";

            ReadRecords(whatTable, ongoingAuctions);
            whatTable = 2;
            ReadRecords(whatTable, auctionsAwaitingPayment);
            whatTable = 3;
            ReadRecords(whatTable, completedAuctions);
        }
    }

    private void ReadRecords(int whatTable, string selectString)
    {

        OleDbConnection conn = null;
        OleDbDataReader reader = null;
     

        try
        {
            conn = new OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=" + "|DataDirectory|BayouFarmersDB.accdb");
            //            conn = new OleDbConnection(ConfigurationManager.AppSettings["ItemsDataSet"]);
            conn.Open();
            
            OleDbCommand cmdSelect = new OleDbCommand(selectString, conn);


            reader = cmdSelect.ExecuteReader();

            //Count the number of items
            int index = 0;
            while (reader.Read())
            {
                AuctionIDNum[index] = (int)reader[0];
                index++;
            }
            reader.Close();

            Rows = index;
            Columns = 7;

            //Fill data arrays
            i = 0;
            reader = cmdSelect.ExecuteReader();
            while (reader.Read())
            {
                AuctionIDNum[i] = (int)reader["ID"];
                Seller[i] = (string)reader["seller"];
                AuctionTitle[i] = (string)reader["title"];
                ItemIDNum[i] = (int)reader["item"];
                Endtime[i] = (DateTime)reader["ends"];
                CurrentWinner[i] = (string)reader["currentWinner"];
                Bidprice[i] = String.Format("{0:0.00}", reader["currentBid"]);
                i++;
            }
            reader.Close();

            //Get webpage address of images.  Must also parse the "description" category to remove the # symbols
            //from the Access database.
            for (i = 0; i < Rows; i++)
            {
                temp = "Select * FROM Items WHERE itemNumber=" + ItemIDNum[i].ToString();
                OleDbCommand cmdPic = new OleDbCommand(temp, conn);
                reader = cmdPic.ExecuteReader();
                while (reader.Read())
                {
                    temp2 = (string)reader["description"];
                    string[] temp3 = temp2.Split('#');
//                    Picture[i] = "<img src=\"" + temp3[1] + "\" height=\"100\" />";
                    Picture[i] = "<img src=\"" + temp3[1] + "\"/>";
                }
            }

            if (Rows > 0)
            {
                CreateDynamicTable(Rows, Columns, whatTable);
            }
        }

        finally
        {
            if (reader != null) reader.Close();
            if (conn != null) conn.Close();
        }
    }

    private void CreateDynamicTable(int tblRows, int tblCols, int whatTable)
    {
        if (whatTable == 1)
        {
            ListTable1.Controls.Clear();
            // Create a Table and set its properties 
            Table tbl = new Table();
            // Add the table to the placeholder control
            ListTable1.Controls.Add(tbl);
            TableRow headerRow = new TableRow();

            for (int j = 0; j < tblCols; j++)
            {
                TableHeaderCell header = new TableHeaderCell();
                if (j == 0)
                {
                    header.Text = "Click To View Auction Details";
                    header.Width = 80;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 1)
                {
                    header.Text = "Seller ID";
                    header.Width = 80;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 2)
                {
                    header.Text = "Auction Title";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 3)
                {
                    header.Text = "End Date/Time";
                    header.Width = 160;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 4)
                {
                    header.Text = "Current Winner";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 5)
                {
                    header.Text = "Current Bid Price";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 6)
                {
                    header.Text = "Picture";
                    header.Width = 100;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }

                header.BorderStyle = BorderStyle.None;
                headerRow.Cells.Add(header);
            }
            tbl.Rows.AddAt(0, headerRow);

            // Now iterate through the table and add your controls 

            for (int i = 0; i < tblRows + 1; i++)
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < tblCols; j++)
                {
                    if (i == tblRows)
                    {
                        TableCell tc = new TableCell();
                        Label title = new Label();

                        if (j == 0)
                        { title.Text = ""; }
                        if (j == 1)
                        { title.Text = ""; }
                        if (j == 2)
                        { title.Text = ""; }
                        if (j == 3)
                        { title.Text = ""; }
                        if (j == 4)
                        { title.Text = ""; }
                        if (j == 5)
                        { title.Text = ""; }
                        if (j == 6)
                        { title.Text = ""; }
                        tc.Controls.Add(title);
                        tr.Cells.Add(tc);
                    }
                    else
                    {
                        TableCell tc = new TableCell();
                        Label title = new Label();

                        if (j == 0)
                        { title.Text = "<a href=\"SpecificAuction.aspx?AuctionID=" + AuctionIDNum[i] + "\">" + AuctionIDNum[i].ToString() + "</a>"; }                        
                        if (j == 1)
                        { title.Text = Seller[i]; }
                        if (j == 2)
                        { title.Text = AuctionTitle[i]; }
                        if (j == 3)
                        { title.Text = Endtime[i].ToString(); }
                        if (j == 4)
                        { title.Text = CurrentWinner[i]; }
                        if (j == 5)
                        { title.Text = Bidprice[i]; }
                        if (j == 6)
                        { 
                            title.Text = Picture[i]; 
//                            tc.Height = 100; 
                        }
                        title.BorderStyle = BorderStyle.None;
                        // Add the control to the TableCell
                        tc.Controls.Add(title);
                        // Add the TableCell to the TableRow
                        tr.Cells.Add(tc);
                    }
                }
                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }//End of if whatTable==1

        if (whatTable == 2)
        {
            ListTable2.Controls.Clear();
            // Create a Table and set its properties 
            Table tbl = new Table();
            // Add the table to the placeholder control
            ListTable2.Controls.Add(tbl);
            TableRow headerRow = new TableRow();

            for (int j = 0; j < tblCols; j++)
            {
                TableHeaderCell header = new TableHeaderCell();
                if (j == 0)
                {
                    header.Text = "Click To Pay";
                    header.Width = 80;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 1)
                {
                    header.Text = "Seller ID";
                    header.Width = 80;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 2)
                {
                    header.Text = "Auction Title";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 3)
                {
                    header.Text = "End Date/Time";
                    header.Width = 160;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 4)
                {
                    header.Text = "Current Winner";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 5)
                {
                    header.Text = "Current Bid Price";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 6)
                {
                    header.Text = "Picture";
                    header.Width = 100;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }

                header.BorderStyle = BorderStyle.None;
                headerRow.Cells.Add(header);
            }
            tbl.Rows.AddAt(0, headerRow);

            // Now iterate through the table and add your controls 

            for (int i = 0; i < tblRows + 1; i++)
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < tblCols; j++)
                {
                    if (i == tblRows)
                    {
                        TableCell tc = new TableCell();
                        Label title = new Label();

                        if (j == 0)
                        { title.Text = ""; }
                        if (j == 1)
                        { title.Text = ""; }
                        if (j == 2)
                        { title.Text = ""; }
                        if (j == 3)
                        { title.Text = ""; }
                        if (j == 4)
                        { title.Text = ""; }
                        if (j == 5)
                        { title.Text = ""; }
                        if (j == 6)
                        { title.Text = ""; }
                        tc.Controls.Add(title);
                        tr.Cells.Add(tc);
                    }
                    else
                    {
                        TableCell tc = new TableCell();
                        Label title = new Label();

                        if (j == 0)
                        { title.Text = "<a href=\"Checkout.aspx?AuctionID=" + AuctionIDNum[i] + "&BuyNow=No\">" + AuctionIDNum[i].ToString() + "</a>"; }
                        if (j == 1)
                        { title.Text = Seller[i]; }
                        if (j == 2)
                        { title.Text = AuctionTitle[i]; }
                        if (j == 3)
                        { title.Text = Endtime[i].ToString(); }
                        if (j == 4)
                        { title.Text = CurrentWinner[i]; }
                        if (j == 5)
                        { title.Text = Bidprice[i]; }
                        if (j == 6)
                        { 
                            title.Text = Picture[i]; 
//                            tc.Height = 100; 
                        }
                        title.BorderStyle = BorderStyle.None;
                        // Add the control to the TableCell
                        tc.Controls.Add(title);
                        // Add the TableCell to the TableRow
                        tr.Cells.Add(tc);
                    }
                }
                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }//End of if whatTable==2

        if (whatTable == 3)
        {
            ListTable3.Controls.Clear();
            // Create a Table and set its properties 
            Table tbl = new Table();
            // Add the table to the placeholder control
            ListTable3.Controls.Add(tbl);
            TableRow headerRow = new TableRow();

            for (int j = 0; j < tblCols; j++)
            {
                TableHeaderCell header = new TableHeaderCell();
                if (j == 0)
                {
                    header.Text = "Auction ID";
                    header.Width = 80;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 1)
                {
                    header.Text = "Seller ID";
                    header.Width = 80;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 2)
                {
                    header.Text = "Auction Title";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 3)
                {
                    header.Text = "End Date/Time";
                    header.Width = 160;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 4)
                {
                    header.Text = "Current Winner";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 5)
                {
                    header.Text = "Your Winning Bid";
                    header.Width = 120;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }
                if (j == 6)
                {
                    header.Text = "Picture";
                    header.Width = 100;
                    header.HorizontalAlign = HorizontalAlign.Left;
                }

                header.BorderStyle = BorderStyle.None;
                headerRow.Cells.Add(header);
            }
            tbl.Rows.AddAt(0, headerRow);

            // Now iterate through the table and add your controls 

            for (int i = 0; i < tblRows + 1; i++)
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < tblCols; j++)
                {
                    if (i == tblRows)
                    {
                        TableCell tc = new TableCell();
                        Label title = new Label();

                        if (j == 0)
                        { title.Text = ""; }
                        if (j == 1)
                        { title.Text = ""; }
                        if (j == 2)
                        { title.Text = ""; }
                        if (j == 3)
                        { title.Text = ""; }
                        if (j == 4)
                        { title.Text = ""; }
                        if (j == 5)
                        { title.Text = ""; }
                        if (j == 6)
                        { title.Text = ""; }
                        tc.Controls.Add(title);
                        tr.Cells.Add(tc);
                    }
                    else
                    {
                        TableCell tc = new TableCell();
                        Label title = new Label();

                        if (j == 0)
                        { title.Text = AuctionIDNum[i].ToString(); }
                        if (j == 1)
                        { title.Text = Seller[i]; }
                        if (j == 2)
                        { title.Text = AuctionTitle[i]; }
                        if (j == 3)
                        { title.Text = Endtime[i].ToString(); }
                        if (j == 4)
                        { title.Text = CurrentWinner[i]; }
                        if (j == 5)
                        { title.Text = Bidprice[i]; }
                        if (j == 6)
                        { 
                            title.Text = Picture[i]; 
//                            tc.Height = 100; 
                        }
                        title.BorderStyle = BorderStyle.None;
                        // Add the control to the TableCell
                        tc.Controls.Add(title);
                        // Add the TableCell to the TableRow
                        tr.Cells.Add(tc);
                    }
                }
                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }//End of if whatTable==3
    }

    protected void exitBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
}